package com.kitty.mybatis.mybatisv1.session;

import com.kitty.mybatis.mybatisv1.executor.Executor;
import com.kitty.mybatis.mybatisv1.executor.statement.KityPreparedStatementHandler;
import com.kitty.mybatis.mybatisv1.mapping.KittyBoundSql;
import com.kitty.mybatis.mybatisv1.mapping.KittyEnvironment;
import com.kitty.mybatis.mybatisv1.mapping.KittyMappedStatement;
import com.kitty.mybatis.mybatisv1.binding.KittyMapperRegistry;
import com.kitty.mybatis.mybatisv1.executor.statement.KittyStatementHandler;
import com.kitty.mybatis.mybatisv1.session.defaults.KittyDefaultSqlSession;

import java.util.HashMap;
import java.util.Map;

/**
 * 主要的配置类
 * @Author SHEN
 * @Date 2020/10/20
 */
public class KittyConfiguration {

    protected KittyEnvironment environment;

    protected KittyMapperRegistry mapperRegistry = new KittyMapperRegistry(this);

    protected Map<String, KittyMappedStatement> mappedStatement = new HashMap<>();

    public KittyConfiguration(KittyEnvironment environment) {
        this.environment = environment;
    }

    public <T> T getMapper(Class<T> clazz, KittyDefaultSqlSession kittyDefaultSqlSession) {
        return mapperRegistry.getMapper(clazz,kittyDefaultSqlSession);
    }

    public <T> void addMapper(Class<T> clazz){

        mapperRegistry.addMapper(clazz);
    }


    public KittyMappedStatement getMappedStatement(String statement) {
        return mappedStatement.get(statement);
    }

    public KittyStatementHandler newStatementHandler(Executor wrapper, KittyMappedStatement ms, Object parameter, KittyBoundSql boundSql) {
        //源码当中使用的是statement路由去根据不同的条件选择创建statementhandler对象，我这里直接创建preparedstatement对象方便查看
        KittyStatementHandler statementHandler = new KityPreparedStatementHandler(wrapper, ms, parameter, boundSql);
        //插件在这里插入
        return statementHandler;
    }
}
